--
-- Author: shunguo.chen
-- Date: 2018-11-17 16:26:33
--
-- TeamMatchListPanel 组队查找器里面的推荐队伍列表

local TeamListRender = import(".renders.TeamListRender")

local ClassRef = app.mvc.createPanelClass(...)

local EMPTY_NONE_STR = L("lua_code_text_317")
local EMPTY_SINGLE_STR = L("lua_code_text_191")
local EMPTY_PVP_STR = L("ui_csd_678")
local LOCK_OPEN_STR = L("lua_code_text_5")

function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.MountPanel))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init()
    return sprite
end

function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

function ClassRef:init(data)
    self.super.init(self, data)

    self._cacheRenders = {}

    self._lockText = self._ui.tips_lock:getChildByName("text")
    self._emptyText = self._ui.tips:getChildByName("text")
    self._ui.Button:addClickEventListener(function()
        executeFunc(self._onOperate, "refreshList", self._target)
    end)
end

function ClassRef:_createRender(idx)
    local render
    if #self._cacheRenders > 0 then
        render = self._cacheRenders[idx]
    end
    if not render then
        render = TeamListRender.create()
        render:retain()
        render:onBtnClick(handler(self, self._onClickRender))
        self._cacheRenders[idx] = render
    end
    return render
end

function ClassRef:_onClickRender(btn, op, ...)
    executeFunc(self._onOperate, op, ...)
end

function ClassRef:remove()
    for i,render in ipairs(self._cacheRenders) do
        render:release()
    end
end

function ClassRef:refreshTarget(target)
    self._target = target

    local isPvp = target:isPvp()
    local isOpen = target:isOpen()
    self._ui.Panel_open:setVisible(isOpen)
    self._ui.tips_lock:setVisible(not isOpen)
    self._ui.tlt:setVisible(not isPvp)

    if isOpen then
        local isSingle = target:isSingle()
        self._ui.Button:setVisible(not isSingle and not isPvp)
        self._ui.btn_tips:setVisible(not isSingle and not isPvp)
    else
        local _, reason = target:isConfOpen(true)
        reason = reason or string.format(LOCK_OPEN_STR, target:getOpenLv())
        self._lockText:setString(reason)
    end


end

function ClassRef:refreshTeamApply(team)
    local items = self._ui.ListView_team:getItems()
    for i,item in ipairs(items) do
        if item:getData() == team then
            item:refreshApply()
        end
    end
end

-- data = {TeamVo, ...}
function ClassRef:refresh()
    local teams = self._data
    local isEmpty = #teams == 0
    self._ui.tips:setVisible(isEmpty)
    self._ui.ListView_team:setVisible(not isEmpty)

    if isEmpty then
        local emptyStr = EMPTY_NONE_STR
        if self._target:isSingle() then
            emptyStr = EMPTY_SINGLE_STR
        elseif self._target:isPvp() then
            emptyStr = EMPTY_PVP_STR
        end

        self._emptyText:setString(emptyStr)
    end

    local listView = self._ui.ListView_team
    listView:stopAllActions()
    if not isEmpty then
        listView:removeAllItems()

        local len = #teams
        local curIdx = 0
        local function addTeam()
            curIdx = curIdx + 1
            if curIdx <= len then
                local render = self:_createRender(curIdx)
                listView:pushBackCustomItem(render)
                render:setData(teams[curIdx])
            else
                listView:stopAllActions()
            end
        end

        listView:schedule(addTeam, 0)
    end
end

return ClassRef